/*-------------------------------------------------------------------------
	created:	2010/09/16  17:50
	filename: 	e:\Project_SVN\Server\GamesScene\GameScene.cpp
	author:		Deeple

	purpose:
---------------------------------------------------------------------------*/

#include "ModMail.h"
#include "async_func.h"
#include "IndexManage.h"

IGlobal* g_pGame = NULL;

extern "C"
{
    void OnHeartBeat(int nBeat)
    {
        // 每小时清理垃圾邮件
        if (nBeat % 3600 == 0)
        {
            g_IndexFinder.CheckDeleteRubish();
        }

        // 每10秒检测列表邮件
        if (nBeat % 10 == 0)
        {
            g_IndexFinder.ProcessListMail();
        }
    }
}

void RegisterCallBack()
{
	REG_PUBLIC_MESSAGE(GM_SEND_MAIL);			 // 发送邮件
	REG_PUBLIC_MESSAGE(GM_MAIL_LIST);			 // 获取邮件列表
	REG_PUBLIC_MESSAGE(GM_READ_MAIL);			 // 查看邮件
	REG_PUBLIC_MESSAGE(GM_DELETE_MAIL);			 // 删除邮件
	REG_PUBLIC_MESSAGE(GM_GET_MAIL_ATTACHMENT);  // 获取邮件附件
	REG_PUBLIC_MESSAGE(GM_REGISTER_PLAYER);      // 邮件系统注册玩家
	REG_PUBLIC_MESSAGE(GM_UNREGPLAYER);          // 邮件系统注销玩家

	REG_PUBLIC_MESSAGE(GM_SEND_MAIL_PROPS);
	REG_PUBLIC_MESSAGE(GM_QUERY_SORT_INFO);

	//这个从db中获取已经有的邮件记录，如果有玩家注册 (暂时改成登陆)，会加一个进去，如果db中没有。
	// 载入位置信息
	g_IndexFinder.LoadLocation(g_pGame);
}

// 发送邮件
void PUB_MSG(GM_SEND_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	const char* Sender = pmsg->StringVal();
	const char* ReceName = pmsg->StringVal();
	const char* Title = pmsg->StringVal();
	const char* MailContent = pmsg->StringVal();
	const char* MailAttach = pmsg->StringVal();
	int bcopper = pmsg->IntVal();
	int copper = pmsg->IntVal();

	// 普通邮件
	if (strlen(ReceName) > 0)
	{
		int RecvPoolId = g_IndexFinder.FindRoleNoCreate(ReceName);
		if (RecvPoolId == -1)
		{
			// 返回错误
			CMsgTyped ret;
			ret.SetInt(0);
			ret.SetInt(receiver);
			ret.SetInt(msgId);
			ret.SetInt(GE_PLAYER_NOT_FOUND);// 查无此人 300
			ret.SetString(MailAttach);
			ret.SetString(Sender);
			ret.SetString(ReceName);
			ret.SetString(Title);
			ret.SetInt(bcopper);
			ret.SetInt(copper);

			g_pGame->SendMessage(id, &ret);

			return;
		}

		//// 给发送者写入邮件 //不必要
		//if (wcslen(Sender) > 0)
		//{
		//	CMsgTyped SendMail;
		//	SendMail.SetInt(receiver);
		//	SendMail.SetInt(msgId);
		//	SendMail.SetInt((int)id.nIndex);
		//	SendMail.SetInt((int)id.nIdent);
		//
		//	SendMail.SetWideStr(Sender);
		//	SendMail.SetString(Title);
		//	SendMail.SetWideStr(Sender);
		//	SendMail.SetWideStr(ReceName);
		//	SendMail.SetString(MailAttach);
		//	SendMail.SetString(MailContent);
		//	SendMail.SetInt(0);// 是否发送回执
		//
		//	// 位置信息
		//	UI32 PoolId = g_IndexFinder.FindRole(Sender);
		//
		//	g_pGame->Call(PoolId , AsyncProc_send_mail, &SendMail);
		//}

		// 给接收者写入邮件
		{
			CMsgTyped SendMail;
			SendMail.SetInt(receiver);
			SendMail.SetInt(msgId);
			SendMail.SetInt((int)id.nIndex);
			SendMail.SetInt((int)id.nIdent);

			SendMail.SetString(ReceName);
			SendMail.SetString(Title);
			SendMail.SetString(strlen(Sender) > 0 ? Sender : "系统"); //wcslen
			SendMail.SetString(ReceName);
			SendMail.SetString(MailAttach);
			SendMail.SetString(MailContent);
			SendMail.SetInt(1);// 是否发送回执
			SendMail.SetInt(bcopper);
			SendMail.SetInt(copper);

			// 位置信息
			g_pGame->Call(RecvPoolId , AsyncProc_send_mail, &SendMail);
		}
	}
	else
	{
		//// 群发邮件，如果需要，另写接口
		//CMsgTyped SendMail;
		//SendMail.SetInt(receiver);
		//SendMail.SetInt(msgId);
		//SendMail.SetInt((int)id.nIndex);
		//SendMail.SetInt((int)id.nIdent);
		//
		//
		//SendMail.SetString(Title);
		//
		//SendMail.SetString(MailAttach);
		//SendMail.SetString(MailContent);
		//SendMail.SetInt(0);// 是否发送回执
		//
		//// 位置信息
		//UI32 PoolId = g_IndexFinder.FindRole(ReceName);
		//
		////. 获取集群的节点列表
		//int poolCount[200];
		//int count = 200;
		//g_pGame->QueryPoolList(2, poolCount, count);
		//
		//for (int i=0; i < 200; i++)
		//{
		//	if (poolCount[i] == 0) break;
		//	g_pGame->Call(poolCount[i] , AsyncProc_send_mailAll, &SendMail);
		//}
	}
}

// 发送邮件，系统邮件
void PUB_MSG(GM_SEND_MAIL_PROPS)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	const char* Sender = pmsg->StringVal(); //wchar_t //WideStrVal
	const char* ReceName = pmsg->StringVal(); //wchar_t //WideStrVal
	const char* Title = pmsg->StringVal();
	const char* MailContent = pmsg->StringVal();
	const char* MailAttach = pmsg->StringVal();
	int reason = pmsg->IntVal();

	// 普通邮件
	if (strlen(ReceName) > 0) //wcslen
	{
		int RecvPoolId = g_IndexFinder.FindRoleNoCreate(ReceName);
		if (RecvPoolId == -1)
		{
			// 返回错误
			CMsgTyped ret;
			ret.SetInt(0);
			ret.SetInt(receiver);
			ret.SetInt(msgId);
			ret.SetInt(GE_PLAYER_NOT_FOUND);// 查无此人 300
			ret.SetInt(reason);
			ret.SetString(MailAttach);
			ret.SetString(Sender);
			ret.SetString(ReceName);
			ret.SetString(Title);

			g_pGame->SendMessage(id, &ret);

			return;
		}

		// 给接收者写入邮件
		CMsgTyped SendMail;
		SendMail.SetInt(receiver);
		SendMail.SetInt(msgId);
		SendMail.SetInt((int)id.nIndex);
		SendMail.SetInt((int)id.nIdent);

		SendMail.SetString(ReceName); //SetWideStr
		SendMail.SetString(Title);
		SendMail.SetString(strlen(Sender) > 0 ? Sender : "系统"); //SendMail.SetWideStr(wcslen(Sender) > 0 ? Sender : Utf8AsWideStr("系统").c_str());
		SendMail.SetString(ReceName); //SetWideStr
		SendMail.SetString(MailAttach);
		SendMail.SetString(MailContent);
		SendMail.SetInt(reason);

		// 位置信息
		g_pGame->Call(RecvPoolId , AsyncProc_send_mail_props, &SendMail);
	}

}

// 获取邮件列表
void PUB_MSG(GM_MAIL_LIST)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	//_Debug("PUB_MSG(GM_MAIL_LIST)");

	CMsgTyped MailList;

	const char* PlayerName = pmsg->StringVal();
	MailList.SetInt( receiver );
	MailList.SetInt(msgId);
	MailList.SetInt((int)id.nIndex);
	MailList.SetInt((int)id.nIdent);
	MailList.SetString(PlayerName);

	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_mail_list, &MailList);

}

// 查看邮件
void PUB_MSG(GM_READ_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	//_Debug("PUB_MSG(GM_READ_MAIL)");

	CMsgTyped ReadMail;
	const char* MailID = pmsg->StringVal();
	const char* PlayerName = pmsg->StringVal();

	ReadMail.SetInt(receiver);
	ReadMail.SetInt(msgId);
	ReadMail.SetInt((int)id.nIndex);
	ReadMail.SetInt((int)id.nIdent);
	ReadMail.SetString(MailID);
	ReadMail.SetString( PlayerName );

	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_read_mail, &ReadMail);

}

// 删除邮件
void PUB_MSG(GM_DELETE_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	//_Debug("PUB_MSG(GM_DELETE_MAIL)");

	CMsgTyped DeleteMail;

	const char* MailID = pmsg->StringVal();
	const char* PlayerName = pmsg->StringVal();

	DeleteMail.SetInt(receiver);
	DeleteMail.SetInt(msgId);
	DeleteMail.SetInt((int)id.nIndex);
	DeleteMail.SetInt((int)id.nIdent);
	DeleteMail.SetString(MailID);
	DeleteMail.SetString(PlayerName);

	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_delete_mail, &DeleteMail);

}

// 获取邮件附件
void PUB_MSG(GM_GET_MAIL_ATTACHMENT)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	//_Debug("PUB_MSG(GM_GET_MAIL_ATTACHMENT)");

	const char* MailID = pmsg->StringVal();
	const char* PlayerName = pmsg->StringVal();

	CMsgTyped Get_Attachment;

	Get_Attachment.SetInt(receiver);
	Get_Attachment.SetInt(msgId);
	Get_Attachment.SetInt((int)id.nIndex);
	Get_Attachment.SetInt((int)id.nIdent);
	Get_Attachment.SetString(MailID);
	Get_Attachment.SetString(PlayerName);

	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_mail_attachment, &Get_Attachment);

}

// 邮件系统注册玩家
void PUB_MSG(GM_REGISTER_PLAYER)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{

	//printf("GM_REGISTER_PLAYER \n");

	const char* PlayerName = pmsg->StringVal(); //wchar_t WideStrVal

	// 注册此玩家
	g_IndexFinder.FindRole(PlayerName);

}

// 邮件系统注销玩家
void PUB_MSG(GM_UNREGPLAYER)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	//printf("GM_UNREGPLAYER \n");

	const char* PlayerName = pmsg->StringVal(); //wchar_t WideStrVal

	// 注册此玩家
	g_IndexFinder.DeleteRole(PlayerName);

}

void PUB_MSG(GM_QUERY_SORT_INFO)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
}


